﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Client.Service;
using GenericModels;
using System.Text.RegularExpressions;


namespace Client
{
    public partial class UserCtlFront: UserControl
    {
        public event UserControlClickHandler InnerButtonClick;
        public delegate void UserControlClickHandler(object sender, EventArgs e);
        public Employee employee { get; set; }

        private IPerson personService;

        

        public UserCtlFront()
        {
            InitializeComponent();

            personService = new PersonClient();

            LoadFrontPic();//load the picture on startup
            //MessageBox.Show("Eneste emp id i db er: 1111111114");
        }


        /// <summary>
        /// Loads the front image
        /// </summary>
        private void LoadFrontPic()
        {
            picBoxFront.ImageLocation = "http://www.mynetimages.com/648ece0b80.jpg";
        }

        /// <summary>
        /// Locates the employee in the database, and sets the employee property
        /// </summary>
        private void LoadEmp(string toVerify)
        {
            int empId = Convert.ToInt32(empIdTextBox.Text);

            employee = personService.GetEmployeeById(empId);
        }

        /// <summary>
        /// Trigger the LoadEmp() and sends event to the GuiUserControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void logInButton_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClick != null)
            {
                string toVerify = empIdTextBox.Text;
                if (toVerify != null & toVerify.Length == 10)
                {
                    if (verifyCpr(toVerify) == true)
                    {
                            LoadEmp(toVerify);
                            if (employee != null)
                            {
                                this.InnerButtonClick(sender, e);
                            }
                            else
                                MessageBox.Show("medarbejder ikke fundet");
                    }
                    else
                    {
                        MessageBox.Show("medarbejder Nr skal bestå af tal");
                    }
                }
                else
                {
                    MessageBox.Show("indtastning skal være 10 karakter lang og bestå af tal!");
                }
            }
        }

        public static bool verifyCpr(string toVerify)
        {
            string pattern = @"([0-9]+)";
                
            return Regex.IsMatch(toVerify, pattern);
        }

        private void empIdTextBox_TextChanged(object sender, EventArgs e)
        {

        }
        

        

    }
}
